package com.ldc.wandroid.ui.activitys;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.IdRes;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.ldc.common.common.CommonViewClick;
import com.ldc.common.mvvm.BaseMvvmActivity;
import com.ldc.common.utils.ViewModelUtils;
import com.ldc.common.utils.ViewUtils;
import com.ldc.wandroid.R;
import com.ldc.wandroid.databinding.ActivityMainBinding;
import com.ldc.wandroid.ui.fragments.HomeFragment;
import com.ldc.wandroid.ui.fragments.MeFragment;
import com.ldc.wandroid.ui.fragments.ProjectTabFragment;
import com.ldc.wandroid.ui.fragments.SquareFragment;
import com.ldc.wandroid.ui.fragments.SystemFragment;
import com.ldc.wandroid.viewmodel.MainViewModel;

public class MainActivity extends BaseMvvmActivity<ActivityMainBinding, MainViewModel> {

    private Fragment indexFragment = new Fragment();
    private HomeFragment homeFragment;
    private SystemFragment systemFragment;
    private ProjectTabFragment projectTabFragment;
    private SquareFragment squareFragment;
    private MeFragment meFragment;

    public static void start(Context context) {
        context.startActivity(new Intent(context, MainActivity.class));
    }


    @Override
    protected ActivityMainBinding initViewBinding() {
        return ActivityMainBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void main(Bundle savedInstanceState) {
        bottomClick();
        // 默认显示项目
        changeFragment(getFragment(R.id.fl_home));
    }


    @Override
    protected MainViewModel initViewModel() {
        return ViewModelUtils.getViewModel(this, MainViewModel.class);
    }


    private void bottomClick() {
        CommonViewClick<View> bottomViewListener = data -> {
            Fragment fragment = getFragment(data.getId());
            changeFragment(fragment);
        };
        ViewUtils.addViewClick(this, R.id.fl_home, bottomViewListener);
        ViewUtils.addViewClick(this, R.id.fl_sys, bottomViewListener);
        ViewUtils.addViewClick(this, R.id.fl_project, bottomViewListener);
        ViewUtils.addViewClick(this, R.id.fl_square, bottomViewListener);
        ViewUtils.addViewClick(this, R.id.fl_me, bottomViewListener);
    }

    /**
     * 钱换页面
     *
     * @param fragment
     */
    private void changeFragment(Fragment fragment) {
        if (null == fragment) {
            return;
        }
        if (fragment != indexFragment) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if (fragment.isAdded()) {
                transaction.show(fragment).hide(indexFragment);
            } else {
                transaction.add(R.id.fragment_container, fragment)
                        .show(fragment)
                        .hide(indexFragment);
            }
            transaction.commit();
        }
        indexFragment = fragment;
    }

    /**
     * 获取fragment
     *
     * @param index id
     * @return Fragment
     */
    private Fragment getFragment(@IdRes int index) {
        switch (index) {
            case R.id.fl_home:
                if (homeFragment == null) {
                    homeFragment = HomeFragment.newInstance();
                }
                return homeFragment;
            case R.id.fl_sys:
                if (systemFragment == null) {
                    systemFragment = SystemFragment.newInstance();
                }
                return systemFragment;
            case R.id.fl_project:
                if (projectTabFragment == null) {
                    projectTabFragment = ProjectTabFragment.newInstance();
                }
                return projectTabFragment;
            case R.id.fl_square:
                if (squareFragment == null) {
                    squareFragment = SquareFragment.newInstance();
                }
                return squareFragment;
            case R.id.fl_me:
                if (meFragment == null) {
                    meFragment = MeFragment.newInstance();
                }
                return meFragment;
            default:
                return null;
        }
    }
}